package com.boang;

import com.boang.dao.SpuDao;
import com.boang.po.Spu;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/*
  @author: 13813
  @date: 2024/7/23 16:40
*/
public class SecDemo {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //-----------------------------sqlSession1-----------------------------------------------------------
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        SpuDao spuDao1 = sqlSession1.getMapper(SpuDao.class);
        Spu spu1 = new Spu(104553, "华为为", null);
        //先从一级缓存查找，如果有，则返回，如果没有，则查询数据库，并把数据放到缓存中
        Spu sp1 = spuDao1.selectSpuByIdAndName(spu1);
        System.out.println(sp1);

        sqlSession1.close();//才放到二级缓存中

        //-----------------------------sqlSession2-----------------------------------------------------------
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        SpuDao spuDao2 = sqlSession2.getMapper(SpuDao.class);
        Spu spu2 = new Spu(104553, "华为为", null);
        //先从一级缓存查找，如果有，则返回，如果没有，则查询数据库，并把数据放到缓存中
        Spu sp2 = spuDao2.selectSpuByIdAndName(spu2);
        System.out.println(sp2);
    }
}
